﻿CREATE PROCEDURE [ServiceEngine].[AssignEndpointToRuntimeServer]
	@RuntimeServerID uniqueidentifier,
	@EndpointID uniqueidentifier
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	BEGIN TRY
		BEGIN TRANSACTION

		DECLARE @ID as uniqueidentifier
		
		SELECT	RowID
		FROM	ServiceEngine.RuntimeServers_Endpoints
		WHERE	RuntimeServerID = @RuntimeServerID
		AND		EndpointID = @EndpointID
		
		IF (@@ROWCOUNT = 0) 
		BEGIN
			SET @ID = newid();

			INSERT	ServiceEngine.RuntimeServers_Endpoints (RowID, RuntimeServerID, EndpointID)
			VALUES	(@ID, @RuntimeServerID, @EndpointID)
		END

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH
END


